﻿using System;
using System.Collections.Generic;
using System.Data;

using CoreLab.Oracle; //using Devart.Data.Oracle; //using Oracle.DataAccess.Client;
//using Oracle.DataAccess.Types;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Oracle;

namespace DBQuery
{
    /// <summary>
    /// Summary description for NotificationTable
    /// </summary>
    public static class NotificationTable
    {
        public static void SetNotificationAsRead(String sxUserId)
        {
            DBLib dbl = new DBLib();
            
            OracleParameter prm = new OracleParameter();
            prm.ParameterName = "prmID";
            prm.OracleDbType = OracleDbType.VarChar;
            prm.Value = sxUserId;

            String sxQuery = "update map_notification_user set IS_READ = 'Y' where ICP_USER_ID = :prmID";

            dbl.ExecuteNonQuery(sxQuery, prm);
            
        }

        public static int InsertNotification_WallPost(int iNewsFeedId)
        {
            DBLib dbl = new DBLib();
            int iReturn = InsertNotification_WallPost(dbl, iNewsFeedId);
            
            return iReturn;
        }
        public static int InsertNotification_WallPost(DBLib dbl, int iNewsFeedId)
        {
            List<OracleParameter> lsParams = new List<OracleParameter>();

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "out_notif_id";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Direction = ParameterDirection.Output;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_T_NEWS_FEED_ID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Value = iNewsFeedId;

            String sxQuery = "PR_INSERT_WALL_POST_NOTIF";

            dbl.ExecuteProcedure(sxQuery, lsParams.ToArray());

            return Convert.ToInt32(lsParams[0].Value);
        }

        public static int InsertNotification_UserEvent(String sxValue, String sxTriggeringUserId, int iCompanyId, 
            UserEventType tpEvent, DateTime dtmEvent)
        {
            DBLib dbl = new DBLib();
            int iReturn = InsertNotification_UserEvent(dbl, sxValue, sxTriggeringUserId, iCompanyId, tpEvent, dtmEvent);

            return iReturn;
        }
        public static int InsertNotification_UserEvent(DBLib dbl, String sxValue, String sxTriggeringUserId, 
            int iCompanyId, UserEventType tpEvent, DateTime dtmEvent)
        {
            List<OracleParameter> lsParams = new List<OracleParameter>();

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "out_new_id";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Direction = ParameterDirection.Output;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_VALUE";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            lsParams[lsParams.Count - 1].Value = sxValue;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_TRIGGERING_USER_ID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            lsParams[lsParams.Count - 1].Value = sxTriggeringUserId;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_TRIGGERING_CUSTOMER_ID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;

            if (iCompanyId < 0)
            {
                lsParams[lsParams.Count - 1].Value = DBNull.Value;
            }
            else
            {
                lsParams[lsParams.Count - 1].Value = iCompanyId;
            }

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_P_USER_EVENT_TYPE_ID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Value = (int)tpEvent;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_EVENT_DATE";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Date;
            lsParams[lsParams.Count - 1].Value = dtmEvent;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_CREATE_BY";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            lsParams[lsParams.Count - 1].Value = ICPUser.ID;

            String sxQuery = "PR_INSERT_T_USER_EVENT";

            dbl.ExecuteProcedure(sxQuery, lsParams.ToArray());

            return Convert.ToInt32(lsParams[0].Value);
        }

        public static void InsertNotification_MapByEvent(int iUserEventId, int iCompanyId)
        {
            DBLib dbl = new DBLib();
            InsertNotification_MapByEvent(dbl, iUserEventId, iCompanyId);
        }
        public static void InsertNotification_MapByEvent(DBLib dbl, int iUserEventId, int iCompanyId)
        {
            List<OracleParameter> lsParams = new List<OracleParameter>();

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_T_USER_EVENT_ID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            lsParams[lsParams.Count - 1].Value = iUserEventId;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_TRIGGERING_CUSTOMER_ID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Value = iCompanyId;

            String sxQuery = "PR_CUSTOMER_EVENT_NOTIFIER_BAK";

            dbl.ExecuteProcedure(sxQuery, lsParams.ToArray());
        }
    }
}